import { getToken, setToken, delToken } from '@/utils/storage'
import { login } from '@/api/user'

const state = {
  token: getToken()
}
const mutations = {
  setToken (state, token) {
    state.token = token
    setToken(token)
  },
  removeToken (state) {
    state.token = ''
    delToken()
  }

}
const actions = {
  async login (ctx, data) {
    const res = await login(data)
    ctx.commit('setToken', res.token)
  },
  logout (ctx) {
    ctx.commit('removeToken')
  }
}
export default {
  namespaced: true,
  state,
  mutations,
  actions
}
